﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
using MixScript.DBService;
using MixScript.BLL.ApplConfig;


namespace MixScript.BLL
{


    public class cBaseBLL : cApplConfig
    {
        public enum statustrx
        {
            create,
            check,
            approve,
            deleted
        }


        public bool posting2Accounting(DataTable dtSlipHDR, DataTable dtSlipdtl, string smatref, out string sout)
        {
            try
            {
                int nret = 0;
                sout = "";
                if (Dbserver.openConn())
                {
                    nret = Dbserver.SqlUpdate(dtSlipHDR, "accsliphdr", out sout);
                    if (nret > 0)
                    {
                        string hdid = Dbserver.ExecuteScalar("select slipid from accsliphdr where matref='" + smatref + "'").ToString();

                        foreach (DataRow dr in dtSlipdtl.Rows)
                        {
                            dr["header_id"] = hdid;
                            //                            dr["id"] = hdid;
                        }
                        // dtSlipdtl.AcceptChanges();
                        nret = Dbserver.SqlUpdate(dtSlipdtl, "accslipdet", out sout);
                        Dbserver.CommitAndCloseConn();
                    }
                    else
                    {
                        Dbserver.ForceRollBackNClose();
                        return false;
                    }
                }
                else
                {
                    sout = "Could not Open Database";
                    return false;

                }

                return nret > 0;

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public string getStatus(statustrx st)
        {
            if (st == statustrx.approve)
            {
                return "A";
            }
            else if (st == statustrx.check)
            {
                return "C";
            }
            else if (st == statustrx.create)
            {
                return "N";
            }
            else if (st == statustrx.deleted)
            {
                return "D";
            }
            else
            {
                return "";
            }

        }
        public DataTable getCountPrint( string docid,int doctype)
        {
            return Dbserver.ExecuteQuery("select * from sy_countprint","sy_countprint");

        }

        public int incCountPrint(int doctype, string docid)
        {
            try
            {
                string s = Dbserver.ExecuteScalar("select cprint from sy_countprint where doctype=" + doctype.ToString() + " and docid='" + docid + "'").ToString();
                int c = 0;
                if (s != "")
                {
                    if (Dbserver.openConn())
                    {
                        Dbserver.ExecuteNonQuery("update sy_countprint set cprint=" + (int.Parse(s) + 1).ToString() + " where doctype=" + doctype.ToString() + " and docid='" + docid + "'");
                        Dbserver.CommitAndCloseConn();
                    }
                }
                else
                {
                    if (Dbserver.openConn())
                    {
                        Dbserver.ExecuteNonQuery("insert into sy_countprint values(" + doctype + ",'" + docid + "',1)");
                        Dbserver.CommitAndCloseConn();
                    }
                    c = 1;

                }


                return c;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int getCountPrint(int doctype, string docid)
        {
            try
            {
                string s = Dbserver.ExecuteScalar("select cprint from sy_countprint where doctype=" + doctype.ToString() + " and docid='" + docid + "'").ToString();
                int c = 0;
                if (s != "")
                {
                    c = int.Parse(s);
                    
                }
                else
                {                  
                    c = 0;
                }


                return c;

            }
            catch (Exception ex)
            {

                throw;
            }
        }

        #region Property
        private int _secLevel = 0;
        private string _status = "";
        private string _procid = "";
        private string _modulname = "";

        private ProcType _prosestype;

        public ProcType ProsesType
        {

            get { return _prosestype; }
            set { _prosestype = value; }
        }


        public string Modulname
        {
            get { return _modulname; }
            set { _modulname = value; }
        }
        public string Procid
        {
            get { return _procid; }
            set { _procid = value; }
        }
        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }
        public int SecLevel
        {
            get { return _secLevel; }
            set { _secLevel = value; }
        }
        #endregion

        public cBaseBLL(string modulname, string procid)
        {

            Modulname = modulname;
            Procid = procid;

            getSecLevel();
            getTrxStatus();
        }
        public cBaseBLL()
        {

            if (Modulname != string.Empty)
            {
                if (Modulname == string.Empty || Procid == string.Empty)
                {
                    Debug.WriteLine("Invalid Modul and processid");
                    Msg = "Invalid Modul and processid";
                    //throw new Exception("Invalid Modul and processid");
                    NoError = false;
                }
                getSecLevel();

                if (!getTrxStatus())
                {
                    Debug.WriteLine("Invalid ProcID");
                    Msg = "Invalid ProcID";
                    //throw new Exception("Invalid Modul and processid");
                    NoError = false;
                }
            }

        }
        private bool getSecLevel()
        {
            try
            {

                cApplConfig oConfig = new cApplConfig();
                SecLevel = oConfig.getProcidLevel(Modulname, Procid, -1);
                return true;
            }
            catch (Exception ex)
            {

                throw;
            }
        }
        private bool getTrxStatus()
        {
            try
            {

                cApplConfig oConfig = new cApplConfig();
                if (SecLevel < 0)
                {
                    Status = oConfig.getLevelProcStatus("ALL", Procid, oConfig.GeneralProcIDLvl);
                }
                else
                {
                    Status = oConfig.getLevelProcStatus(Modulname, Procid, SecLevel);
                }

                Msg = Status;
                if (Status.Length == 3)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception)
            {

                throw;
            }

        }



    }




}
